package com.sample.servicefragment;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

/**
 * 一个支持下拉抽屉效果的DummyFragment类
 */
public abstract class SlidingDrawer extends DummyServiceFragment {
    private static final String TAG = "SlidingDrawer";

    private static final int ORIENTATION_INITIAL = -2;
    private static final int ORIENTATION_UP = -1;
    private static final int ORIENTATION_NONE = 0;
    private static final int ORIENTATION_DOWN = 1;

    private int mWindowHeight = -1;

    private View mContentView = null;
    private int mContentViewX = -1;
    private int mContentViewY = -1;
    private int mContentViewHeight = -1;
    private int mMoveOrientation = ORIENTATION_NONE;

    @Override
    public void addView() {
        mContentView = onCreateView();
        super.setView(mContentView);
        WindowManager.LayoutParams layoutParams = createLayoutParams();
        super.setLayoutParams(layoutParams);
        mWindowHeight = getLayoutParams().height;
        mContentView.setVisibility(View.GONE);
        mContentView.setOnTouchListener(mStatusBarOnTouchListener);
        mContentView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                Handler handler = new Handler();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mContentView.setY(-mWindowHeight);
                    }
                });
            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });

        super.addView();
    }

    public View.OnTouchListener getOnTouchListener() {
        return mStatusBarOnTouchListener;
    }

    private View.OnTouchListener mStatusBarOnTouchListener =  new View.OnTouchListener() {
        private float  mStartY = 0.0f;
        private float  mCurrentY = 0.0f;
        private float mLastY = mStartY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "===" + event.toString());
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: // 捕获手指触摸按下动作
                    mMoveOrientation = ORIENTATION_INITIAL;
                    mContentView.setVisibility(View.VISIBLE);
                    mStartY = event.getY();
                    mLastY = mStartY;
                    mContentViewY = (int)mContentView.getY();
                    mContentViewX = (int)mContentView.getX();
                    mContentViewHeight = (int)mContentView.getHeight();
                    break;
                case MotionEvent.ACTION_MOVE:
                    mCurrentY = event.getRawY();
                    if(mCurrentY > mLastY) {
                        mMoveOrientation = ORIENTATION_DOWN;
                    } else if(mCurrentY < mLastY) {
                        mMoveOrientation = ORIENTATION_UP;
                    } else {
                        mMoveOrientation = ORIENTATION_NONE;
                    }
                    Log.d(TAG, "===event.orientation:" + mMoveOrientation);
                    slideDrawer((int)(mCurrentY - mStartY));
                    mLastY = mCurrentY;
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d(TAG, "===event.orientation:" + mMoveOrientation);
                    mCurrentY = event.getRawY();
                    slideDrawerEnd((int)(mCurrentY - mStartY));
                    mStartY=0;
                    break;
                default:
                    break;
            }
            return true;
        }
    };

    private void slideDrawer(int offsetY) {
        if(!isExpanded) { // 此时接受鼠标事件的是statusbar，panel随着statusbar鼠标事件被拖动。
            if ((mContentViewY + offsetY) <= 0) {
                if ((mContentViewY + offsetY) >= -mContentViewHeight) {
                    mContentView.setY(mContentViewY + offsetY);
                } else {
                    mContentView.setY(-mContentViewHeight);
                }
            } else {
                mContentView.setY(0);
            }
        } else {
            if ((mContentViewY + offsetY) <= 0) {
                if ((mContentViewY + offsetY) >= -mContentViewHeight) {
                    mContentView.setY(mContentViewY + offsetY);
                } else {
                    mContentView.setY(-mContentViewHeight);
                }
            } else {
                mContentView.setY(0);
            }
        }
    }

    private Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if(!isExpanded) {
                mContentView.setVisibility(View.GONE);
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };

    private boolean isExpanded = false;
    private void slideDrawerEnd(int offsetY) {
        float translationY = mContentView.getTranslationY();
        if (!isExpanded) { // 未展开时
            if(ORIENTATION_UP == mMoveOrientation || ORIENTATION_NONE == mMoveOrientation
                || ORIENTATION_INITIAL == mMoveOrientation) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(mContentView,
                        "translationY",translationY, -mContentViewHeight).setDuration(500);
                animator.addListener(mAnimatorListener);
                animator.start();
            } else {
                ObjectAnimator.ofFloat(mContentView,"translationY",translationY, 0)
                        .setDuration(500).start();
                isExpanded = true;
            }
        } else {
            if (ORIENTATION_UP == mMoveOrientation) {
                isExpanded = false;
                ObjectAnimator animator = ObjectAnimator.ofFloat(mContentView,"translationY",translationY,
                        -mContentViewHeight).setDuration(500);
                animator.addListener(mAnimatorListener);
                animator.start();
            } else {
                ObjectAnimator.ofFloat(mContentView,"translationY",translationY, 0)
                        .setDuration(500).start();
            }
        }
    }

    public void close() {
        if(isExpanded) {
            mMoveOrientation = ORIENTATION_UP;
            slideDrawerEnd(-1);
        }
    }
}
